let React = require('react');

var Join = React.createClass({
  getInitialState: function () {
    return {
      type: "sign_in"
    };
  },
  handleClick: function (event) {
    switch (event.target.id) {
    case "sign-mode-switch":
      var signType = this.state.type == "sign_up" ? "sign_in" : "sign_up";
      this.setState({
        type: signType
      });
      break;
    default:
      break;
    }
  },
  handleSignAction: function (event) {
    var username = $('#username').val();
    var password = $('#password').val();

    if (this.state.type == "sign_up") {
      $.getJSON(baseUrl + "signUp/" + username + "/" + password + "/1", function (data, status) {
        console.log(data);
        if (status == "success") {
          switch (data.code) {
          case 900:
            break;
          case 800:
          case 811:
            alert(data.message);
            break;
          default:
          }
        }
      });
    } else {
      $.getJSON(baseUrl + "signIn/" + username + "/" + password, function (data, status) {
        console.log(data);
        if (status == "success") {
          switch (data.code) {
          case 910:
          case 911:
            location.href = "blog.html";
            break;
          case 810:
            alert(data.message);
            break;
          default:
          }
        }
      });
    }
  },
  render: function () {
    var signButtonText = this.state.type == "sign_up" ? "注册" : "登录";
    var signSwitchText = this.state.type == "sign_up" ? "已经有账号？点击登录" : "还没有账号？现在注册";
    return (
      <div className="join-container">
        <div className="join-box">
          <form>
            <input defaultValue="lkk" id="username" placeholder="用户名" type="text"/>
            <input defaultValue="12345" id="password" placeholder="密码" type="password"/>
            <input id="sign-submit" onClick={this.handleSignAction} type="button" value={signButtonText}/>
          </form>
          <a href="#" id="sign-mode-switch" onClick={this.handleClick}>{signSwitchText}</a>
        </div>
      </div>
    );
  }
});

module.exports = Join;
